<?php 
	$area=$_POST['txtArea'];
	$mes = $_POST['txtMes'];
	$ano = $_POST['txtAgno'];
	$uf = $_POST['txtUF2'];
	$dia = $_POST['txtDia'];
	$cliente = $_POST['txtCliente'];
	$porcentaje = $_POST['txtPorcentaje'];
	
	header('Content-type: application/vnd.ms-excel');
	header("Content-Disposition: attachment; filename=CalculoPsmb-".$cliente."-".$mes."-".$ano.".xls");
	header("Pragma: no-cache");
	header("Expires: 0");
	require_once("../conexion/conexion.php");
	require_once("../Funciones/funciones_publicas.php");
	require_once("../Mantenedores/AreaExtraccion.php");
	$ae = new AreaExtraccion();
	$nomAE = $ae->BuscaAE($area);
	$fun = new funciones_publicas();
	$palmes = $fun->SeleccionaMes($mes);
	
	function BuscaPrecios($area,$analisis)
	{
		$con = new Conexion();
		$con2 = $con->conexionBD("fpproduccion");
		$sql="select ValPrecio from valor
				where TSusNum='".$analisis."' and AECod='".$area."'";
		$res= mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
		$precio = mysql_fetch_array($res);
		$con->cerrarBD();
		return $precio[0];
	}
	function BuscaPrecioMuestreo($area)
	{
		$con = new Conexion();
		$con2 = $con->conexionBD("fpproduccion");
		$sql="select ValPrecio from valor
				where TSusNum=0 and AECod='".$area."'";
		$res= mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
		$precio = mysql_fetch_array($res);
		$con->cerrarBD();
		return $precio[0];
	}
	function BuscaEstacion($area,$num)
	{
		$con = new Conexion();
		$con2 = $con->conexionBD("fpproduccion");
		$sql="select EsNombre from estacion e
				inner join centroestacion ce on
				ce.EsCod=e.EsCod
				inner join centrocultivo cc on
				ce.Cod=cc.Cod
				inner join formulario f on
				(f.Cod=cc.Cod and f.AECod=cc.AECod)
				where ForNumero='".$num."' and f.AECod='".$area."'";
		$res= mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
		$est = mysql_fetch_array($res);
		$con->cerrarBD();
		return $est[0];
	}
	
	function ObtenerSemana($dia)
	{
	$arreglo = array();
	//sabemos en que dia comienza el mes, para conocer las semanas
	$primerdia = $dia;
	//echo $primerdia;
	if($primerdia==0)//domingo
	{
		$diaIni = 1;
		$diaFin = 1;
		$sem1 = 1;//semana 1 va desde 1 a 1
		$arreglo[0] = 1;
		$arreglo[1] = 1;
	}
	if($primerdia==1)//lunes
	{
		$diaIni = 1;
		$diaFin = 7;
		$sem1 = 1;//semana 1 va desde 1 a 7
		$arreglo[0] = 1;
		$arreglo[1] = 7;
	}
	if($primerdia==2)//martes
	{
		$diaIni = 1;
		$diaFin = 6;
		$sem1 = 1;//semana 1 va desde 1 a 6
		$arreglo[0] = 1;
		$arreglo[1] = 6;
	}
	if($primerdia==3)//miercoles
	{
		$diaIni = 1;
		$diaFin = 5;
		$sem1 = 1;//semana 1 va desde 1 a 5
		$arreglo[0] = 1;
		$arreglo[1] = 5;
	}
	if($primerdia==4)//jueves
	{
		$diaIni = 1;
		$diaFin = 4;
		$sem1 = 1;//semana 1 va desde 1 a 4
		$arreglo[0] = 1;
		$arreglo[1] = 4;
	}
	if($primerdia==5)//viernes
	{
		$diaIni = 1;
		$diaFin = 3;
		$sem1 = 1;//semana 1 va desde 1 a 3
		$arreglo[0] = 1;
		$arreglo[1] = 3;
	}
	if($primerdia==6)//sabado
	{
		$diaIni = 1;
		$diaFin = 2;
		$sem1 = 1;//semana 1 va desde 1 a 2
		$arreglo[0] = 1;
		$arreglo[1] = 2;
	}
	return $arreglo;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>
<style>
.txt
{
	font-family:Verdana, Arial, Helvetica, sans-serif;
	font-size:10px;
}
</style>
</head>

<body>

<table width="75%" border="1" cellpadding="0" cellspacing="0" >
<tr>
<td colspan="6"><div align="center" style="font-size:12px"><strong>MEMORIA DE CALCULO MUESTREO PSMB AREA <?php echo $nomAE;?> , MES <?php echo $palmes;?> DEL <?php echo $ano;?></strong></div></td>
</tr>
<tr>
  <td colspan="6" class="txt"><strong>Cliente: <?php echo $cliente;?>&nbsp;/&nbsp;<?php echo $porcentaje;?>% de Pago</strong></td>
</tr>
<tr>
<td colspan="6" class="txt"><strong>Valor UF: <?php echo $uf;?></strong></td>
</tr>
<tr>
    <th width="10%" class="txt">fecha</th>
    <th width="15%" class="txt">formulario</th>
    <th width="20%" class="txt">An&aacute;lisis</th>
	<th width="18%" class="txt">Estaci&oacute;n</th>
    <th width="19%" class="txt">V. Unitario($)</th>
	<th width="18%" class="txt">V. Total($)</th>
</tr>
<?php
		$var = ObtenerSemana($dia);
		//echo $var[0].' '.$var[1];
		$sem = 1;
		while($sem<6)
		{
		$fechaIni = $ano.'-'.$mes.'-'.$var[0];
		$fechaFin = $ano.'-'.$mes.'-'.$var[1];
		$con = new Conexion();
		$con2 = $con->conexionBD("fpproduccion");
//		date_format(formulario.MFecha,'%d-%m-%y') as fecha,
//		date_format(ForFecha,get_format(date,'eur')) as fecha,
		$sql="select
			date_format(ForFecha,'%d-%m-%Y') as fecha,
			f.ForNumero,
			TSusNombre,
			a.TSusNum
			
			from formulario f
			inner join analisis a on
			a.ForNumero=f.ForNumero
			inner join tiposustancia ts on
			ts.TSusNum = a.TSusNum
			
			where 
			(AECod = $area and (DATE(ForFecha) BETWEEN '".$fechaIni."' and '".$fechaFin."'))
			order by ForFecha asc";		
		$res= mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
		$fila = mysql_num_rows($res);
		//echo $fila;
		$con->cerrarBD();
		if($fila >0)
		{
		echo "<tr>";
		echo "<td style='text-align:center;' colspan='5' class='txt'><label style='color:#000000;'><strong>SEMANA ".$sem."</strong></label></td>";
		echo "</tr>";
		while($reg = mysql_fetch_array($res))
		{
			$preciomuestreo = BuscaPrecioMuestreo($area);
			$est = BuscaEstacion($area,$reg['ForNumero']);
			$precios = BuscaPrecios($area,$reg['TSusNum']);
			$final = $precios*$uf;
			$valfinal += $final;

				echo "<tr>";
				echo "<td width=\"10%\" style='text-align:center;' class='txt'>".$reg['fecha']."</td>";
				echo "<td width=\"15%\" style='text-align:center;' class='txt'>".$reg['ForNumero']."</td>";
				echo "<td width=\"20%\" style='text-align:center;' class='txt'>".$reg['TSusNombre']."</td>";
				echo "<td width=\"18%\" style='text-align:center;' class='txt'>".$est."</td>";
				echo "<td width=\"19%\" style='text-align:center;' class='txt'>".$precios."</td>";
				echo "<td width=\"18%\" style='text-align:center;' class='txt'>".number_format($final,2,',','.')."</td>";
				echo "</tr>";
		}//fin while
				$muetreo = ($preciomuestreo*$uf);
				$valfinal += $muetreo;
				echo "<tr>";
				echo "<td width=\"10%\" style='text-align:center;' class='txt'>&nbsp;</td>";
				echo "<td width=\"15%\" style='text-align:center;' class='txt'>&nbsp;</td>";
				echo "<td width=\"20%\" style='text-align:center;' class='txt'>&nbsp;</td>";
				echo "<td width=\"18%\" style='text-align:center;' class='txt'>MUESTREO</td>";
				echo "<td width=\"19%\" style='text-align:center;' class='txt'>".$preciomuestreo."</td>";
				echo "<td width=\"18%\" style='text-align:center;' class='txt'>".number_format($muetreo,2,',','.')."</td>";
				echo "</tr>";
		 }//fin if fila
        $sem++;
        $var[0]=$var[1]+1;
        $var[1]+=7;
		
		//saber el ultimo dia del mes
		$ultimodia=date("j",mktime(0,0,0,$mes+1,0,$ano));
		//echo $ultimodia;
		if($var[1]>=$ultimodia)
		{
			$var[1]=$ultimodia;
		}
		
        }//fin while semana
			$valfinal = round($valfinal);
			$iva = round($valfinal*0.19);
			$total = $iva+$valfinal;
				echo "<tr>";
				echo "<td style='text-align:center;' colspan='4' class='txt'>&nbsp;</td>";
				echo "<td width=\"19%\" style='text-align:right; padding-right:10px' class='txt'><label style='color:#000000;'><strong>TOTAL NETO ($)</strong></label></td>";
				echo "<td width=\"18%\" style='text-align:right; padding-right:40px' class='txt'><label style='color:#000000;'><strong>".number_format($valfinal,0,',','.')."</strong></label></td>";
				echo "</tr>";
				echo "<tr>";
				echo "<td style='text-align:center;' colspan='4' class='txt'>&nbsp;</td>";
				echo "<td width=\"19%\" style='text-align:right; padding-right:10px' class='txt'><label style='color:#000000;'><strong>IVA (19%) ($)</strong></label></td>";
				echo "<td width=\"18%\" style='text-align:right; padding-right:40px' class='txt'><label style='color:#000000;'><strong>".number_format($iva,0,',','.')."</strong></label></td>";
				echo "</tr>";
				echo "<tr>";
				echo "<td style='text-align:center;' colspan='4' class='txt'>&nbsp;</td>";
				echo "<td width=\"19%\" style='text-align:right; padding-right:10px' class='txt'><label style='color:#000000;'><strong>TOTAL ($)</strong></label></td>";
				echo "<td width=\"18%\" style='text-align:right; padding-right:40px' class='txt'><label style='color:#000000;'><strong>".number_format($total,0,',','.')."</strong></label></td>";
				echo "</tr>";
				
				////////////////////////////////////////////////////////////////////////////////////////////
				// PORCENTAJE QUE PAGA EL CLIENTE ///
				////////////////////////////////////////////////////////////////////////////////////////////
				
				echo "<tr>";
				echo "<td style='text-align:center;' colspan='4' class='txt'>&nbsp;</td>";
				echo "<td width=\"19%\" style='text-align:right; padding-right:10px' class='txt'><label style='color:#000000; font-weight:bold;'>$porcentaje% PAGO</label></td>";
				echo "<td width=\"18%\" style='text-align:right; padding-right:40px' class='txt'>&nbsp;</td>";
				echo "</tr>";
			
				$valfinal2 = round((($valfinal*$porcentaje)/100),0);
				$iva2 = round($valfinal2*0.19);
				$total2 = $iva2+$valfinal2;
				
				echo "<tr>";
				echo "<td style='text-align:center;' colspan='4' class='txt'>&nbsp;</td>";
				echo "<td width=\"19%\" style='text-align:right; padding-right:10px' class='txt'><label style='color:#000000;'><strong>TOTAL NETO ($)</strong></label></td>";
				echo "<td width=\"18%\" style='text-align:right; padding-right:40px' class='txt'><label style='color:#000000;'><strong>".number_format($valfinal2,0,',','.')."</strong></label></td>";
				echo "</tr>";
				echo "<tr>";
				echo "<td style='text-align:center;' colspan='4' class='txt'>&nbsp;</td>";
				echo "<td width=\"19%\" style='text-align:right; padding-right:10px' class='txt'><label style='color:#000000;'><strong>IVA (19%) ($)</strong></label></td>";
				echo "<td width=\"18%\" style='text-align:right; padding-right:40px' class='txt'><label style='color:#000000;'><strong>".number_format($iva2,0,',','.')."</strong></label></td>";
				echo "</tr>";
				echo "<tr>";
				echo "<td style='text-align:center;' colspan='4' class='txt'>&nbsp;</td>";
				echo "<td width=\"19%\" style='text-align:right; padding-right:10px' class='txt'><label style='color:#000000;'><strong>TOTAL ($)</strong></label></td>";
				echo "<td width=\"18%\" style='text-align:right; padding-right:40px' class='txt'><label style='color:#000000;'><strong>".number_format($total2,0,',','.')."</strong></label></td>";
				echo "</tr>";
		?>
		</table>
</body>
</html>